import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_tindercard/flutter_tindercard.dart';
import 'package:shishi/bean/songcibean.dart';

import '../Colors.dart';

class SongCiPage extends StatefulWidget {
  @override
  _SongCiPageState createState() => _SongCiPageState();
}

class _SongCiPageState extends State<SongCiPage> {

  int _page = 1;
  SongCi _songCi;
  List<songciuser> _list = new List<songciuser>();
  List _tags =[];
  // ignore: non_constant_identifier_names
  void GetSongCi() async {
    try {
      Response response = await Dio().get(
          "http://api.tianapi.com/txapi/songci/index?key=a69400da230d90d3d4ed644b6d3d1fb7&num=10&page=$_page");
      _songCi = new SongCi(response.data);
      _list.addAll(_songCi.newslist);

      setState(() {});
      _page++;
    } catch (e) {
      print(e);
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    GetSongCi();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('精选宋词'),
      ),
      body: body(),
    );
  }

  Widget body() {
    if(_list.length > 0) {
      return TinderSwapCard(
          orientation: AmassOrientation.TOP,
          totalNum: 200,
          stackNum: 4,
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height * 0.85,
          minWidth: MediaQuery.of(context).size.width * 0.8,
          minHeight: MediaQuery.of(context).size.height * 0.7,

          cardBuilder: (context, index) {
            return GestureDetector(
                child: Card(
                  elevation: 10,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadiusDirectional.circular(10)),
                  clipBehavior: Clip.antiAlias,
                  child: SingleChildScrollView(
                    child: Column(
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(top: 15, left: 10, right: 10),
                          child: Text(
                            '${_list[index].title}',
                            style: TextStyle(
                              fontFamily: 'yuwei',
                              fontSize: 30,
                            ),
                          ),
                        ),
                        Text(
                          '${_list[index].author}',
                          style: TextStyle(
                              fontFamily: 'lixuke',
                              fontSize: 18,
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.all(18),
                          child: Text(
                            '${_list[index].content}',
                            style: TextStyle(fontSize: 20, fontFamily: 'kai'),
                          ),
                        ),
                        Container(
                            margin: EdgeInsets.all(10),
                            child: Text(
                              '${_list[index].yiwen}',
                            )),
                        Container(
                          child: Wrap(
                            children: tags.toList(),
                          ),
                        )
                      ],
                    ),
                  ),
                ),

                onDoubleTap: () {
                },
              );
          },
          swipeUpdateCallback: (DragUpdateDetails details, Alignment align) {},
          swipeCompleteCallback: (CardSwipeOrientation orientation, int index) {
            _tags = _list[index+1].tags.split(',');
            if (index == _list.length - 4) {
              GetSongCi();
            }
          });
    } else {
      return SpinKitRipple(
        color: Cos.zhutise,
      );
    }
  }

  List<Color> bgcolors = [
    Colors.red,
    Colors.greenAccent,
    Colors.pink,
    Colors.lightGreenAccent,
    Colors.green[200]
  ];

  Iterable<Widget> get tags sync* {
    for (int i = 0; i < _tags.length; i++) {
      yield Container(
          margin: EdgeInsets.only(left: 5),
          decoration: BoxDecoration(borderRadius: BorderRadius.circular((10))),
          child: Chip(
            backgroundColor: bgcolors[1],
            label: Text(
              '#${_tags[i]}',
              style: TextStyle(
                color: Colors.white,
                fontSize: 14,
              ),
            ),
            labelPadding: EdgeInsets.only(left: 1.0, right: 1.0),
            materialTapTargetSize: MaterialTapTargetSize.padded,
          ));
    }
  }

}
